package com.empire.module.member.convert.user;

import com.empire.framework.common.pojo.PageResult;
import com.empire.module.member.api.user.dto.MemberUserRespDTO;
import com.empire.module.member.controller.admin.user.vo.MemberUserRespVO;
import com.empire.module.member.controller.admin.user.vo.MemberUserSaveReqVO;
import com.empire.module.member.controller.admin.user.vo.MemberUserUpdateReqVO;
import com.empire.module.member.controller.admin.user.vo.PersonCurInfoVo;
import com.empire.module.member.dal.dataobject.group.MemberGroupDO;
import com.empire.module.member.dal.dataobject.tag.MemberTagDO;
import com.empire.module.member.dal.dataobject.user.MemberUserDO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Map;

import static com.empire.framework.common.util.collection.CollectionUtils.convertList;
import static com.empire.framework.common.util.collection.CollectionUtils.convertMap;

@Mapper
public interface MemberUserConvert {

    MemberUserConvert INSTANCE = Mappers.getMapper(MemberUserConvert.class);

    MemberUserDO convert(MemberUserSaveReqVO bean);

    MemberUserRespDTO convert2(MemberUserDO bean);

    List<MemberUserRespDTO> convertList2(List<MemberUserDO> list);

    // 显式指定字段映射
    @Mapping(source = "idcard", target = "idcard")
    MemberUserDO convert(MemberUserUpdateReqVO bean);

    PageResult<MemberUserRespVO> convertPage(PageResult<MemberUserDO> page);

    PageResult<PersonCurInfoVo> convertCurPage(PageResult<MemberUserDO> page, List<MemberGroupDO> groups);

    MemberUserRespVO convert03(MemberUserDO bean);

    default PageResult<MemberUserRespVO> convertPage(PageResult<MemberUserDO> pageResult,
                                                     List<MemberTagDO> tags,
                                                     List<MemberGroupDO> groups) {
        PageResult<MemberUserRespVO> result = convertPage(pageResult);
        // 填充关联数据
        result.getList().forEach(user -> convert2FullVo(user, tags, groups));
        return result;
    }

    default PageResult<PersonCurInfoVo> convertCurInfoPage(PageResult<MemberUserDO> pageResult,
                                                           List<MemberGroupDO> groups) {
        PageResult<PersonCurInfoVo> result = convertCurPage(pageResult, groups);

        result.getList().forEach(user -> convert2CaseInfoFullVo(user, groups));
        return result;
    }

    default void convert2FullVo(MemberUserRespVO user,
                                List<MemberTagDO> tags,
                                List<MemberGroupDO> groups) {
        // 处理关联数据
        if (tags != null && !tags.isEmpty()) {
            Map<Long, String> tagMap = convertMap(tags, MemberTagDO::getId, MemberTagDO::getName);
            user.setTagNames(convertList(user.getTagIds(), tagMap::get));
        }
        if (groups != null && !groups.isEmpty()) {
            Map<Long, String> groupMap = convertMap(groups, MemberGroupDO::getId, MemberGroupDO::getName);
            user.setGroupName(groupMap.get(user.getGroupId()));

        }
    }

    default void convert2CaseInfoFullVo(PersonCurInfoVo user,
                                List<MemberGroupDO> groups) {
        // 处理关联数据
        if (groups != null && !groups.isEmpty()) {
            Map<Long, String> groupMap = convertMap(groups, MemberGroupDO::getId, MemberGroupDO::getName);
            user.setGroupName(groupMap.get(user.getGroupId()));

        }
    }

}
